<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平滚动条拖动</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .scroll {
            width: 300px;
            height: 8px;
            background-color: #cccccc;
            margin: 100px auto;
            position: relative;
        }

        .bar {
            width: 10px;
            height: 20px;
            background-color: #369;
            position: absolute;
            top: -6px;
            left: 0px;
            cursor: pointer;
        }

        .selected {
            width: 0;
            background-color: #369;
            height: 100%;
        }
    </style>
</head>
<body>
<div class="scroll">
    <div class="bar"></div>
    <div class="selected"></div>
</div>

<div class="progress"></div>
</body>
</html>

<script>
    var scroll = document.getElementsByClassName("scroll")[0];
    var bar = scroll.children[0];
    var mask = scroll.children[1];

    var progress = document.getElementsByClassName("progress")[0];

    bar.onmousedown = function (event) {
        document.onmousemove = function (e) {
            var event = e || window.event;
            var x = event.clientX - scroll.offsetLeft;
            if (x < 0) {
                x = 0;
            }
            if (x > scroll.offsetWidth - bar.offsetWidth) {
                x = scroll.offsetWidth - bar.offsetWidth;
            }
            bar.style.left = x + "px";
            mask.style.width = x+bar.offsetWidth+ "px";
            progress.innerHTML = "您已拖动:" + parseInt((x+bar.offsetWidth) / scroll.offsetWidth * 100) + "%";
        }
    }

    document.onmouseup = function () {
        document.onmousemove = null;
    }


</script>